# Copyright 2009 http://www.yuntien.com
# Licensed under the Apache License, Version 2.0

from ytblog.controller.common import *

class BlogDisplayController(BlogController):
    
  def render(self, context):
    path = os.path.join(os.path.dirname(__file__), ytblog.config.TEMPLATE_DIR + 'display.html')
    self.response.out.write(template.render(path, context))    

  def get_comments(self, blog):
    query = Comment.all().filter('blog =', blog)
    if ytblog.config.COMMENT_DESCENDING:
      query = query.order('-date')
    return self.get_page_objects(query, ytblog.config.COMMENT_NUM_PER_PAGE)
      
  #display a particular blog
  @check_blog_id
  def get(self, id):
    blog = Blog.get_by_str_id(id)
    blog = self.fill_blog_info(blog)
    comments, page = self.get_comments(blog)
    
    for comment in comments:
      comment.date += datetime.timedelta(hours = ytblog.config.TIMEZONE_OFFSET)
      comment.auth = authorization.get_auth((Comment, comment))
    
    context = {
      'blog': blog,
      'comments': comments,
      'page': page,
      }
    self.add_common_context(context)
    self.render(context)
